﻿using Domain.Entity;
using Domain.Repository;
using Microsoft.Data.SqlClient;
using Domain.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace EFCore.Repository
{
    public class CFSKRepository : ICFSKRepository
    {
        private readonly MyDbContext myDbContext;

        public CFSKRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public async Task<int> AddCFSK(CFSK cfSK)
        {
           myDbContext.CFSK.Add(cfSK);
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<int> DeleteCFSK(int id)
        {
            var CFSK=await myDbContext.CFSK.FindAsync(id);
            if (CFSK != null)
            {
                myDbContext.CFSK.Remove(CFSK);
                return await myDbContext.SaveChangesAsync();
            }
            return 0;
        }

        public List<CFSK> GetCFSK()
        {
            using (SqlConnection connection = new SqlConnection("server=.;database=HR;user=sa;pwd=cssl#123;Encrypt=False;"))
            {
                string sql = "select * from dbo.CFSK  ";
                return connection.Query<CFSK>(sql).ToList();
            }
        }

		public List<CFSK> GetCFSKName(string name)
		{
			var user = myDbContext.CFSK.Where(e => e.FirstKindName == name).ToList();
			return user;
		}

		public async Task<int> UpdateCFSK(CFSK cfSK)
        {
            myDbContext.CFSK.Update(cfSK);
            int result= await myDbContext.SaveChangesAsync();
            return result;
        }
    }
}
